package com.zhj.leetcode;

/**
 * 力扣209 长度最小的子数组
 * @author zhj
 */
public class Test209 {

    public static void main(String[] args) {
        int[] nums = {2,3,1,2,4,3};
        System.out.println(mumsLength(nums, 7));
    }

    private static int mumsLength(int[] nums, int sum) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int res = nums.length + 1;
        int total = 0;
        int i = 0;
        int j = 0;
        while (j < nums.length) {
            total = total + nums[j];
            j++;
            while (total >= sum) {
                res = res < j-i ? res : j-i;
                total = total - nums[i];
                i = i + 1;
            }
        }
        if (res == nums.length + 1) {
            return 0;
        }
        return res;
    }
}
